package com.xieyingwangluo.yxpart.logic;

import com.xieyingwangluo.yxpart.bean.Result;
import com.xieyingwangluo.yxpart.bean.UserEduDTO;
import com.xieyingwangluo.yxpart.entity.UserEducationModel;
import com.xieyingwangluo.yxpart.service.UserEduService;
import com.xieyingwangluo.yxpart.utils.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class UserEduLogic {
    @Autowired
    private UserEduService userEduService;

    public Result add(UserEduDTO userEduDTO) {
        UserEducationModel userEducationModel = new UserEducationModel();
        BeanUtils.copyProperties(userEduDTO, userEducationModel);
        userEducationModel.setEnrollment(DateUtil.StD(userEduDTO.getEnrollment(), "yyyy-MM-dd HH:mm"));
        userEducationModel.setGraduation(DateUtil.StD(userEduDTO.getGraduation(), "yyyy-MM-dd HH:mm"));
//        BeanUtils.copyProperties(A,B);
//        是A中的值付给B
        if (userEducationModel != null) {
            int cnt = userEduService.add(userEducationModel);
            if (cnt > 0) {
                return Result.generate(0, "Add success", userEducationModel);
            }
        }
        return Result.generate(1, "Add fail", null);
    }

    public Result getById(String noid) {
        if (noid != null || !noid.equals("")) {
            UserEducationModel userEducationModel = userEduService.getById(noid);
            return Result.generate(0, "Select success", userEducationModel);
        }
        return Result.generate(1, "Select fail", null);
    }

    public Result upd(String noid, UserEduDTO userEduDTO) {
        UserEducationModel userEducationModel = new UserEducationModel();
        BeanUtils.copyProperties(userEduDTO, userEducationModel);
        userEducationModel.setNoid(noid);
        if (userEduDTO.getEnrollment() != null && !userEduDTO.getEnrollment().equals("")) {
            userEducationModel.setEnrollment(DateUtil.StD(userEduDTO.getEnrollment(), "yyyy-MM-dd HH:mm"));
        }
        if (userEduDTO.getGraduation() != null && !userEduDTO.getGraduation().equals("")) {
            userEducationModel.setGraduation(DateUtil.StD(userEduDTO.getGraduation(), "yyyy-MM-dd HH:mm"));
        }
        int cnt = userEduService.upd(userEducationModel);
        if (cnt > 0) {
            return Result.generate(0, "Upd success", cnt);
        }
        return Result.generate(1, "Upd fail", null);
    }

    public Result selA( UserEduDTO userEduDTO) {
        UserEducationModel userEducationModel = new UserEducationModel();
        BeanUtils.copyProperties(userEduDTO, userEducationModel);
        if (userEduDTO.getEnrollment() != null && !userEduDTO.getEnrollment().equals("")) {
            userEducationModel.setEnrollment(DateUtil.StD(userEduDTO.getEnrollment(), "yyyy-MM-dd HH:mm"));
        }
        if (userEduDTO.getGraduation() != null && !userEduDTO.getGraduation().equals("")) {
            userEducationModel.setGraduation(DateUtil.StD(userEduDTO.getGraduation(), "yyyy-MM-dd HH:mm"));
        }
        List<UserEducationModel> list = userEduService.selA(userEducationModel);
        System.out.println(list);
        if (list.size() > 0) {
            return Result.generate(0, "Select success", list);
        }
        return Result.generate(1, "Select fail", null);
    }
}
